318. 最大单词长度乘积
https://leetcode-cn.com/problems/maximum-product-of-word-lengths/
- 提交时间:2021-11-17 14:50:31
- 执行用时:12 ms, 在所有 Go 提交中击败了93.01%的用户
- 内存消耗:6.5 MB, 在所有 Go 提交中击败了46.85%的用户
- 通过测试用例:167 / 167
func maxProduct(words []string) (ans int) {
wordsBitArr := make([]int, len(words))
for index, word := range words {
wordsBitArr[index] = 0
for _, ch := range word {
//1. 数组默认值为0
//2. 1 << (ch - 'a'),代表将1向左移动0-25位
//3. |=是求且,将每一位的比特值合并
wordsBitArr[index] |= 1 << (ch - 'a')
}
}
n := len(words)
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
if wordsBitArr[i]&wordsBitArr[j] == 0 {
tmp := len(words[i]) * len(words[j])
if tmp > ans {
ans = tmp
}
}
}
}
return
}